#!/usr/bin/env python
# Copyright (c) 2014 Eugene Frolov <efrolov@mirantis.com>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import json

from restalchemy.api import controllers

from rdb.dm import models
from rdb.old_app.core import storage


class Version(controllers.Controller):
    """This controller which handles "/" url."""

    def filter(self):
        return ['v1']


class Collection(controllers.Controller):
    """This controller which handles "/v1/" url."""

    def filter(self):
        return []


class Customer(controllers.Controller):

    __resource__ = models.Customer

    def __init__(self, *args, **kwargs):
        super(Customer, self).__init__(*args, **kwargs)
        self.rn_storage = storage.RepairNoteStorage()

    def process_result(self, result, status_code=200, headers={},
                       add_location=False):
        return super(Customer, self).process_result(
            result=result, status_code=status_code, headers=headers,
            add_location=False)

    def create(self, name, group, **kwargs):
        customer = self.rn_storage.create_customer(name, group)
        return json.loads(customer.json())

    def delete(self, uuid, **kwargs):
        self.rn_storage.delete_customer(id=uuid)
        return None, 204

    def update(self, uuid, name, group, **kwargs):
        customer = self.rn_storage.update_customer(
            uuid, name, group)
        return json.loads(customer.json())

    def filter(self, start, limit, **kwargs):
        customers = self.rn_storage.get_customers_list(
            offset=start, limit=limit)
        return json.loads(customers.json())
